<?php
namespace App\Http\Services;


use App\Http\Model\Gift;
use App\Http\Model\Mall;
use App\Http\Model\Room;
use App\Http\Model\RoomMicro;
use App\Http\Model\RoomUser;
use App\Http\Model\User;
use App\Http\Model\Hint;
use App\Http\Model\RoomWaitbit;
use App\Http\Model\RoomHostMicro;
use App\Http\Model\RoomUserProtect;
use App\Http\Model\Union;
use App\Http\Model\UnionUser;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;
use App\Http\Services\NewRoomMicroServices;

class RoomMicroServices extends CommonServices
{
    protected $model_room_user;
    protected $model_user;
    protected $model_room;
    protected $model_room_micro;
    protected $model_room_hostmicro;
    protected $model_room_waitbit;
    protected $micro_lock_key = "micro";
    protected $model_mall;
    protected $model_gift;
    protected $crossRoomPkAnchorPkMission = 'CROSS:ROOM:PK:MISSION:ANCHORMIC';//HASH 主播上麦每日PK缓存 CROSS:ROOM:PK:MISSION:ANCHORMIC {$userId}=>time();
    protected $union_user_model;//公会成员
    protected $union_model;//公会
    protected $micUserProtectModel;//守护麦
    protected $newRoomMicroService;

    //拍拍房记录缓存
    protected $roomPaiPaiKey = 'ROOMPAIPAI:LOG:';//hash ROOMPAIPAI:LOG:{roomId} => {string}=>{string}

    public function __construct()
    {
        $this->model_room_user = new RoomUser();
        $this->model_user = new User();
        $this->model_room = new Room();
        $this->model_room_micro = new RoomMicro();
        $this->model_room_hostmicro = new RoomHostMicro();
        $this->model_room_waitbit = new RoomWaitbit();
        $this->model_mall = new Mall();
        $this->model_gift = new Gift();
        $this->union_user_model = new UnionUser();
        $this->union_model = new Union();
        $this->micUserProtectModel = new RoomUserProtect();
        $this->newRoomMicroService = new NewRoomMicroServices();

    }


    /**
     * 主持人下麦踢出麦
     * @return array
     */
    public function hostMicroOut($roomId){
        return [];
    }
    



    /**
     * 语音平台返回下麦消息
     * @param $room_id
     * @param $user_id
     * @return boolean
     */
    public function callback_out($room_id, $user_id, &$micro_id){
        $micro_info = $this->model_room_micro->get_user_micro_status($room_id, $user_id);
        if ($micro_info === null){
            return false;
        }
        $micro_id = $micro_info['micro_id'];
        $this->model_room_micro->del_user_micro_status($room_id, $user_id);
        $this->model_room_micro->del_user_id($room_id, intval($micro_id));
        //$this->model_room_micro->set_into_begin($room_id, $user_id, $micro_id);
        redis_unlock($this->micro_lock_key.$room_id);

        //删除跨房PK每日任务主播上麦记录
        Redis::DEL($this->crossRoomPkAnchorPkMission);

        return true;
    }


    /**
     * 语音平台返回上麦消息
     * @param $room_id
     * @param $user_id
     * @return bool
     */
    public function callback_into($room_id, $user_id, &$micro_id){
        $micro_id = $this->model_room_micro->get_into_begin($room_id, $user_id);
        if ($micro_id === null){
            return false;
        }
        $this->model_room_micro->set_user_micro_status($room_id, $user_id, $micro_id, 1);
        $this->model_room_micro->set_user_id($room_id, intval($micro_id), $user_id);
        $this->model_room_micro->del_into_begin($room_id, $user_id);
        redis_unlock($this->micro_lock_key.$room_id);

        //跨房PK每日任务
        $this->crossRoomPkMissionMic($user_id, $room_id);
        return true;
    }

    //跨房PK主播上麦每日任务
    public function crossRoomPkMissionMic($user_id, $room_id){
        //每日主播上麦任务PK
        $isAnchor = 0;
        $res = $this->union_user_model->userToUnion($user_id);
        if(!empty($res)){
            //查看公会是否认证
            $checkStatus = $this->union_model->getUnions([$res['un_id']]);
            if(isset($checkStatus[0]['authentication_status']) && $checkStatus[0]['authentication_status'] != 0){
                $isAnchor = 1;
            }
        }
        //查寻房间是否在PK中
        $roomMode = $this->model_room->getRoomOpenList($room_id, 'roomMode');
        if(empty(Redis::HGET($this->crossRoomPkAnchorPkMission,$user_id)) && $isAnchor && $roomMode == 6){
            Redis::HSET($this->crossRoomPkAnchorPkMission,$user_id,time());
            //计算结束当天所差秒数
            $now = Carbon::now()->toDateTimeString();
            $endDay = Carbon::now()->endOfDay()->toDateTimeString();
            $diffTime = Carbon::parse($now)->diffInSeconds($endDay);
            Redis::EXPIRE($this->crossRoomPkAnchorPkMission,$diffTime);//当前时间到今日截止时间
        }
    }


    
    /**
     * 获得麦列表
     * @param $room_id
     * @return array|int
     */
    public function get_micros($room_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }

        $room = $this->model_room->get_room($room_id);
        $typeId = $room['room_type']['id'];
        $micros = $this->model_room_micro->get_micros($room_id, $typeId); //room type从房间获得。
        for ($i = 0; $i < count($micros); $i++){
            if($micros[$i]["user_id"] !== null){
                $micros[$i]["user"] = $this->model_user->get_user($micros[$i]["user_id"]);
                $micros[$i]["user"]['attires'] = $this->model_mall->getGoodsAttire($micros[$i]["user_id"]);
            }
            else{
                $micros[$i]["user"] = null;
            }
            unset($micros[$i]["user_id"]);
        }
        return $micros;
    }


    /**
     * 抱上麦
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     * @return bool|int
     */
    public function into_req($room_id, $micro_id, $user_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        if ($this->model_room->get_role($room_id, $current_user['user_id']) == 0){
            return Hint::AUTH_ERROR;
        }
        //获取房间类型
        $roomInfo = $this->model_room->get_room($room_id);
        
        $micro = $this->model_room_micro->get_micro($room_id, $micro_id);
        if($roomInfo['room_type']['id'] == 44){ //电台类型
            //查询用户是否在房间开通了守护
            $checkInfo = $this->micUserProtectModel->getUserProtectType($room_id, $user_id);
            if(empty($checkInfo)){
                return Hint::ROOM_MICRO_GUARD_NOT_EXIST;
            }
            //麦位对应守护检查
            $protectWithMicId = [
                '1' => [0,1,2,3,4,5,6,7], //黄金对应所有位置
                '2' => [1,2,3,4,5,6,7],//白银对应1,2,3,4,5,6,7位
                '3' => [4,5,6,7],//青铜对应4,5,6,7位
            ];
            //定位用户开通的麦位
            $micCanSeat = $protectWithMicId[$checkInfo['protect_type']];
            if(!in_array($micro['microphone_id'],$micCanSeat)){
                return Hint::ROOM_MICRO_GUARD_MATCH;
            }
        }
        if ($micro === null){
            return Hint::ROOM_MICRO_NOT_NULL;
        }
        if ($micro['user_id'] !== null){
            return Hint::ROOM_MICRO_NOT_NULL;
        }
        if (!$this->model_room_user->exists($room_id, $user_id)){
            return Hint::ROOM_USER_NOT_EXIST;
        }
        if($micro['lock']){
            return Hint::ROOM_MICRO_IS_LOCK;
        }
        if ($this->model_room_micro->get_user_micro_status($room_id, $user_id) !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        if ($this->model_room_hostmicro->get_user_status($room_id, $user_id) !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        if (!redis_lock($this->micro_lock_key.$room_id, 5)){
            return Hint::NOT_ALLOWED;
        }
        $this->model_room_micro->set_into_begin($room_id, $user_id, $micro_id);
        $this->model_room_micro->set_hold($room_id, $micro_id, $user_id);
        //$this->model_room_micro->set_user_id($room_id, $micro_id, $user_id);
        //$this->model_room_micro->set_user_micro_status($room_id, $user_id, $micro_id, 0);
        //$this->model_room_micro->set_into_begin($room_id, $user_id, $micro_id);
        return true;
    }

    /**
     * 设置麦状态锁定
     * @param $room_id
     * @param $micro_id
     * @return bool|int
     */
    public function lock($room_id, $micro_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        if ($this->model_room->get_role($room_id, $user_id) == 0){
            return Hint::AUTH_ERROR;
        }
        $micro = $this->model_room_micro->get_micro($room_id, $micro_id);
        if ($micro['user_id'] !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        $this->model_room_micro->lock_close($room_id, $micro_id);
        return true;
    }

    /**
     * 设置麦状态解锁
     * @param $room_id
     * @param $micro_id
     * @return bool|int
     */
    public function unlock($room_id, $micro_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        if ($this->model_room->get_role($room_id, $user_id) == 0){
            return Hint::AUTH_ERROR;
        }
        $micro = $this->model_room_micro->get_micro($room_id, $micro_id);
        if ($micro['user_id'] !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        $this->model_room_micro->lock_open($room_id, $micro_id);
        return true;
    }


    /**
     * 设置boss位置
     * @param $room_id
     * @param $micro_id
     * @return bool|int
     */
    public function setboss($room_id, $micro_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        if ($this->model_room->get_role($room_id, $user_id) == 0){
            return Hint::AUTH_ERROR;
        }

        $this->model_room_micro->boss_open($room_id, $micro_id);
        return true;
    }

    /**
     * 取消boss位置
     * @param $room_id
     * @param $micro_id
     * @return bool|int
     */
    public function unsetboss($room_id, $micro_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        if ($this->model_room->get_role($room_id, $user_id) == 0){
            return Hint::AUTH_ERROR;
        }
        $this->model_room_micro->boss_close($room_id, $micro_id);
        return true;
    }

    /**
     * 打开麦克风
     * @param $room_id
     * @param $micro_id
     * @param $self true自己;false所有
     * @return bool|int
     */
    public function open($room_id, $micro_id, $self = true){
        //若房间处于全麦封禁状态则不允许操作
        if($this->model_room->get_volume_closed($room_id) == 1){
            return Hint::ROOM_MIC_FORBID;
        }

        //临时处理听歌房不允许操作
        $room = $this->model_room->get_room($room_id);
        $typeId = $room['room_type']['id'];
        if ($typeId == 7){
            return Hint::AUTH_ERROR;
        }

        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        //如果是自己检查禁麦
        if($self == true){
            $blockMicro = $this->model_user->getMicroBlockStatus($current_user['user_id']);
            if ($blockMicro == true){
                return Hint::USER_MICRO_IS_BLOCK;
            }
        }
        
        $user_id = $current_user['user_id'];
        $role = $this->model_room->get_role($room_id, $user_id);

        if (!$self && $role > 0){ //主持人操作
            if ($micro_id > -1){
                $this->model_room_micro->volume_open($room_id, $micro_id);
            }
            else{
                $room = $this->model_room->get_room($room_id);
                $typeId = $room['room_type']['id'];
                $micros = $this->model_room_micro->get_micros($room_id, $typeId);
                for ($i = 0; $i < count($micros); $i++){
                    $this->model_room_micro->volume_open($room_id, $micros[$i]['microphone_id']);
                }
            }
            return true;
        }

        $micro = $this->model_room_micro->get_micro($room_id, $micro_id);
        if ($self && !empty($micro) && $micro['user_id'] == $user_id && $micro['volume']){ //用户自己闭麦
            $this->model_room_micro->volume_self_open($room_id, $micro_id);
            return true;
        }

        return Hint::AUTH_ERROR;
    }

    /**
     * 关闭麦克风
     * @param $room_id
     * @param $micro_id
     * @param $self
     * @return bool|int
     */
    public function close($room_id, $micro_id, $self = true){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        $role = $this->model_room->get_role($room_id, $user_id);

        if (!$self && $role > 0){ //主持人操作
            if ($micro_id > -1){
                $this->model_room_micro->volume_close($room_id, $micro_id);
            }
            else{
                $room = $this->model_room->get_room($room_id);
                $typeId = $room['room_type']['id'];
                $micros = $this->model_room_micro->get_micros($room_id, $typeId);
                for ($i = 0; $i < count($micros); $i++){
                    $this->model_room_micro->volume_close($room_id, $micros[$i]['microphone_id']);
                }
            }
            return true;
        }

        $micro = $this->model_room_micro->get_micro($room_id, $micro_id);
        if ($self && !empty($micro) && $micro['user_id'] == $user_id && $micro['volume']){ //用户自己闭麦
            $this->model_room_micro->volume_self_close($room_id, $micro_id);
            return true;
        }

        return Hint::AUTH_ERROR;
    }


    public function volume_close($room_id, $micro_id){
        $this->model_room_micro->volume_close($room_id, $micro_id);
    }

    public function volume_self_close($room_id, $micro_id){
        $this->model_room_micro->volume_self_close($room_id, $micro_id);
    }


    /**
     * 标记礼物数据
     * @param $room_id
     * @param $user_id
     */
    public function mark_gift($room_id, $send_user_id, $user_id, $gift_id, $gift_num){
        $status = $this->model_room_micro->get_user_micro_status($room_id, $user_id);
        if (empty($status)){
            return null;
        }
        $user = $this->model_user->get_user($send_user_id);
        if (empty($user)){
            return null;
        }
        $user_image = null;
        if (count($user["images"]) > 0) {
            $user_image = $user["images"][0];
        }
        $old_gift_data = $this->model_room_micro->get_gift($room_id, $status['micro_id']);
        $gift = $this->model_gift->get_gift($gift_id);
        if (empty($old_gift_data)){
            $this->model_room_micro->set_gift($room_id, $user['user_id'], $user['nickname'], $user_image, $status['micro_id'], $gift_id, $gift['name'], $gift['image'], $gift_num, intval($gift['price']));
            return $status['micro_id'];
        }
        $old_price = intval($old_gift_data['price']);
        $old_num = intval($old_gift_data['num']);
        $price = intval($gift['price']);
        if($old_price * $old_num < $gift_num * $price){
            $this->model_room_micro->set_gift($room_id, $user['user_id'], $user['nickname'], $user_image, $status['micro_id'], $gift_id, $gift['name'], $gift['image'], $gift_num, intval($gift['price']));
            return $status['micro_id'];
        }
        return null;
    }


    /**
     * 取消设置礼物
     * @param $room_id
     * @return bool
     */
    public function unmark_gift($room_id, $micro_id){
        $gift_data = $this->model_room_micro->get_gift($room_id, $micro_id);
        if (!empty($gift_data)){
            $this->model_room_micro->del_gift($room_id, $micro_id);
            return true;
        }
        return false;
    }

    public function into($room_id, $micro_id, &$user_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        if (!redis_lock($this->micro_lock_key.$room_id, 5)){
            return Hint::NOT_ALLOWED;
        }
        $user_id = $current_user['user_id'];
        $micro = $this->model_room_micro->get_micro($room_id, $micro_id);

        //判断是否锁麦
        $microLock = $this->model_room_micro->getRoomLockMicroByUserId($user_id);
        if(!empty($microLock)) {
            if($microLock['room_id'] != $room_id) {
                return Hint::ROOM_MICRO_USER_LOCK;
            }
            if($microLock['micro_id'] != $micro_id) {
                return Hint::ROOM_MICRO_USER_LOCK;
            }
            $this->model_room_micro->set_into_begin($room_id, $user_id, $micro_id);
            return true;
        }
        //拍拍房
        $cacheKey = $this->roomPaiPaiKey.$room_id;
        $info = Redis::HGETALL($cacheKey);
        if(!empty($info)) {
            $paipaiMicroList = empty($info['micro_list']) ? [] : json_decode($info['micro_list'], true);
            if(isset($paipaiMicroList[$micro_id]) && $paipaiMicroList[$micro_id]==$user_id) {
                $this->model_room_micro->set_into_begin($room_id, $user_id, $micro_id);
                return true;
            }
        }

        //获取房间类型
        $roomInfo = $this->model_room->get_room($room_id);
        if($roomInfo['room_type']['id'] == 44){ //电台类型
            //查询用户是否在房间开通了守护
            $checkInfo = $this->micUserProtectModel->getUserProtectType($room_id, $user_id);
        
            if(empty($checkInfo)){
                return Hint::ROOM_MICRO_GUARD_NOT_EXIST;
            }
            //麦位对应守护检查
            $protectWithMicId = [
                '1' => [0,1,2,3,4,5,6,7], //黄金对应1号位
                '2' => [1,2,3,4,5,6,7],//白银对应2,3,4位
                '3' => [4,5,6,7],//青铜对应5,6,7,8位
            ];
            //定位用户开通的麦位
            $micCanSeat = $protectWithMicId[$checkInfo['protect_type']];
            if(!in_array($micro['microphone_id'],$micCanSeat)){
                return Hint::ROOM_MICRO_GUARD_MATCH;
            }
        }

        if ($micro === null){
            return Hint::ROOM_MICRO_NOT_NULL;
        }
        if ($micro['user_id'] !== null){
            return Hint::ROOM_MICRO_NOT_NULL;
        }
        if (!$this->model_room_user->exists($room_id, $user_id)){
            return Hint::ROOM_USER_NOT_EXIST;
        }
        if($micro['lock']){
            return Hint::ROOM_MICRO_IS_LOCK;
        }
        if ($this->model_room_micro->get_user_micro_status($room_id, $user_id) !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        if ($this->model_room_hostmicro->get_user_status($room_id, $user_id) !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        $this->model_room_micro->set_into_begin($room_id, $user_id, $micro_id);
        return true;
    }


    /**
     * 用户跳麦接口
     * @param $room_id
     * @param $jump_micro_id
     * @param $micro_id
     * @param $user_id
     * @return int
     */
    public function jump($room_id, $jump_micro_id, &$micro_id, &$user_id, &$volume, &$volume_self){

        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        if (!redis_lock($this->micro_lock_key.$room_id, 5)){
            return Hint::NOT_ALLOWED;
        }
        $user_id = $current_user['user_id'];
        $user_micro_info = $this->model_room_micro->get_user_micro_status($room_id, $user_id);
        if (empty($user_micro_info)){
            redis_unlock($this->micro_lock_key.$room_id);
            return Hint::ROOM_MICRO_USER_NOT_EXIST;
        }

        $micro_id = $user_micro_info['micro_id'];
        $micro = $this->model_room_micro->get_micro($room_id, $micro_id);
        $jump_micro = $this->model_room_micro->get_micro($room_id, $jump_micro_id);

        //校验
        $checkRes = $this->newRoomMicroService->checkRoomUserCanQuit($room_id, $user_id);
        if($checkRes['code'] != Hint::SUCCESS) {
            if($checkRes['code'] == Hint::ROOM_MICRO_USER_LOCK) {
                return Hint::ROOM_MICRO_USER_LOCK;
            }
            if($checkRes['code'] == Hint::ROOM_PAIPAI_LOCK) {
                return Hint::ROOM_PAIPAI_LOCK;
            }
        }

        //获取房间类型
        $roomInfo = $this->model_room->get_room($room_id);
        if($roomInfo['room_type']['id'] == 44){ //电台类型
            //查询用户是否在房间开通了守护
            $checkInfo = $this->micUserProtectModel->getUserProtectType($room_id, $user_id);
            if(empty($checkInfo)){
                return Hint::ROOM_MICRO_GUARD_NOT_EXIST;
            }
            //麦位对应守护检查
            $protectWithMicId = [
                '1' => [0,1,2,3,4,5,6,7], //黄金对应所有位置
                '2' => [1,2,3,4,5,6,7],//白银对应1,2,3,4,5,6,7位
                '3' => [4,5,6,7],//青铜对应4,5,6,7位
            ];
            //定位用户开通的麦位
            $micCanSeat = $protectWithMicId[$checkInfo['protect_type']];
            if(!in_array($jump_micro_id,$micCanSeat)){
                return Hint::ROOM_MICRO_GUARD_MATCH;
            }
        }
        
        if ($jump_micro === null){
            redis_unlock($this->micro_lock_key.$room_id);
            return Hint::ROOM_MICRO_NOT_NULL;
        }
        if ($jump_micro['user_id'] !== null){
            redis_unlock($this->micro_lock_key.$room_id);
            return Hint::ROOM_MICRO_NOT_NULL;
        }
        if($jump_micro['lock']){
            redis_unlock($this->micro_lock_key.$room_id);
            return Hint::ROOM_MICRO_IS_LOCK;
        }

        $this->model_room_micro->del_user_id($room_id, intval($micro_id));
        $this->model_room_micro->set_user_micro_status($room_id, $user_id, $jump_micro_id, 1);
        $this->model_room_micro->set_user_id($room_id, intval($jump_micro_id), $user_id);
        if ($micro['volume']){
            $this->model_room_micro->volume_open($room_id, $jump_micro_id);
            $volume = true;
        }else{
            $this->model_room_micro->volume_close($room_id, $jump_micro_id);
            $volume = false;
        }
        if ($micro['volume_self']){
            $this->model_room_micro->volume_self_open($room_id, $jump_micro_id);
            $volume_self = true;
        }else{
            $this->model_room_micro->volume_self_close($room_id, $jump_micro_id);
            $volume_self = false;
        }

        redis_unlock($this->micro_lock_key.$room_id);
        return true;

    }

    /**
     * 获取删除抱麦
     * @param $room_id
     * @return mixed|null
     */
    public function get_hold($room_id, $micro_id){
        return $this->model_room_micro->get_hold($room_id, $micro_id);
    }


    /**
     * 设置删除抱麦
     * @param $room_id
     * @return mixed|null
     */
    public function del_hold($room_id, $micro_id){
        $this->model_room_micro->del_hold($room_id, $micro_id);
    }

    /**根据user_id 获取是否上麦
     * @param $room_id
     * @param $user_id
     * @param $host_user_id
     * @return bool
     */
    public function get_user_micro_status($room_id ,$user_id,$host_user_id){
        if(empty($user_id)){
            $user_id = $this->model_user->get_current_user()['user_id'];
        }
        if($host_user_id == $user_id){
            if($this->model_room_hostmicro->get_user_status($room_id, $host_user_id)){
                return  true;
            }
        }
        $micro_status = $this->model_room_micro->get_user_micro_status($room_id, $user_id);
        if ($micro_status === null){
            return false;
        }
        return true;
    }

    /**打开麦克风
     * @param $room_id
     * @param $user_id
     * @return array|int|mixed|null
     */
    public function h5_open($room_id, $user_id){
        $mic_ids = [];
        $room = $this->model_room->get_room($room_id);
        $typeId = $room['room_type']['id'];
        if ($typeId == 7){
            return Hint::AUTH_ERROR;
        }
        $micros = $this->model_room_micro->get_micros($room_id, $typeId);
        //user_id =-1 普通麦全部开启
        if($user_id == -1){
            for ($i = 0; $i < count($micros); $i++){
                if(!empty($micros[$i]['user_id'])){
                    $mic_ids[] = $micros[$i]['microphone_id'];
                }
                $this->model_room_micro->volume_open($room_id, $micros[$i]['microphone_id']);
            }
            $this->model_room_micro->delGameSpeakMic($room_id);
            return array('mic_id'=>-2 ,'data'=>$mic_ids);
        }
        //主持操作 普通麦全部关闭
        $host_user_id = $this->model_room_hostmicro->get_user_id($room_id);

        if(!empty($host_user_id) && $host_user_id == $user_id){
            for ($i = 0; $i < count($micros); $i++){
                if(!empty($micros[$i]['user_id'])){
                    $mic_ids[] = $micros[$i]['microphone_id'];
                }
                $this->model_room_micro->volume_close($room_id, $micros[$i]['microphone_id']);
            }
            $this->model_room_micro->setGameSpeakMic($room_id ,$user_id);
            return array('mic_id'=>-1 ,'data'=>$mic_ids);
        }
        //普通麦 开启当前用户麦 关闭其他人
        $user_mic = $this->model_room_micro->get_user_micro_status($room_id ,$user_id);
        if(!empty($user_mic)){
            $micro_id = $user_mic['micro_id'];

            for ($i = 0; $i < count($micros); $i++){
                if($i == $micro_id){
                    $this->model_room_micro->volume_open($room_id, $micros[$i]['microphone_id']);
                }else{
                    $this->model_room_micro->volume_close($room_id, $micros[$i]['microphone_id']);
                }
                if(!empty($micros[$i]['user_id'])){
                    $mic_ids[] = $micros[$i]['microphone_id'];
                }
            }
            $this->model_room_micro->setGameSpeakMic($room_id ,$user_id);
            return array('mic_id'=>$micro_id ,'data'=>$mic_ids);

        }
        return null;
    }

    /**关闭麦克风
     * @param $room_id
     * @param $user_id
     * @return array
     */
    public function h5_close($room_id, $user_id){
        $mic_ids = [];
        $room = $this->model_room->get_room($room_id);
        $typeId = $room['room_type']['id'];
        $micros = $this->model_room_micro->get_micros($room_id, $typeId);
        //user_id = -1 普通麦全部关闭
        if($user_id == -1){
            for ($i = 0; $i < count($micros); $i++){
                if(!empty($micros[$i]['user_id'])){
                    $mic_ids[] = $micros[$i]['microphone_id'];
                }
                $this->model_room_micro->volume_close($room_id, $micros[$i]['microphone_id']);
            }
            $this->model_room_micro->delGameSpeakMic($room_id);
            return array('mic_id'=>-1 ,'data'=>$mic_ids);
        }
        $user_mic = $this->model_room_micro->get_user_micro_status($room_id ,$user_id);
        if(!empty($user_mic)){
            $micro_id = $user_mic['micro_id'];
            for ($i = 0; $i < count($micros); $i++){
                if(!empty($micros[$i]['user_id'])){
                    $mic_ids[] = $micros[$i]['microphone_id'];
                }
                if($i == $micro_id){
                    $this->model_room_micro->volume_close($room_id, $micros[$i]['microphone_id']);
                }else{
                    $this->model_room_micro->volume_open($room_id, $micros[$i]['microphone_id']);
                }
            }
            return array('mic_id'=>$micro_id ,'data'=>$mic_ids);
        }
        return [];
    }

}